<?php

class money{
	public $linkk;
	public $all_id=array();
	public $arr_fx_money=array();
	public $all_money=0;
	
	//连接数据库参数
	function __construct($db_host,$db_user,$db_pwd,$db_name,$db_char){
		$this->linkk = mysqli_connect($db_host,$db_user,$db_pwd);
		mysqli_select_db($this->linkk,$db_name);
		mysqli_set_charset($this->linkk,$db_char);
	}
	
	//根据一个月份返回时间戳范围
	function date_int($date){
		$arr=explode('-',$date);
		if($arr[1]==12){
			$date_end=($arr[0]+1)."-1";
		}else{
			$date_end=$arr[0]."-".($arr[1]+1);
		}
		$int_start=strtotime($date);
		$int_end=strtotime($date_end);
		$int="xftime_int>".$int_start." and xftime_int<".$int_end;
//		echo $date;
//		echo "<hr>";
//		echo $date_end;
//		echo "<hr>";
//		echo $int;
		return $int;
	}
	
	//根据id查询他自己的消费总和
	function self_xf($id){
		$sql="select sum(money) from xiaofei where uid=$id;";
		$re= mysqli_query($this->linkk,$sql);
		$row=mysqli_fetch_assoc($re);
		mysqli_free_result($re);
		return $row['sum(money)'];
	}
	
	//根据id查询他所有下级的消费总和
	function all_xf($id,$i=0){
		if($i==0){
			$this->all_money=0;
		}
		$i++;
		$sql="select * from user where pid=$id";
		$re= mysqli_query($this->linkk,$sql);
		$arr =array();
		while($row=mysqli_fetch_assoc($re)){
			$arr[]=$row;
		}
		mysqli_free_result($re);
//		mysqli_close($this->linkk);
		foreach($arr as $v){
//			$this->all_id[]=$v['id'];
			$this->all_money += $this->self_xf($v['id']);
			$this->all_xf($v['id'],$i);
		}
		return $this->all_money;
	}
	
	//根据id查询他的下级id集合
	function arr_id($id,$i=0){
		$i++;
		$sql="select * from user where pid=$id";
		$re= mysqli_query($this->linkk,$sql);
		$arr =array();
		while($row=mysqli_fetch_assoc($re)){
			$arr[]=$row;
		}
		mysqli_free_result($re);
		foreach($arr as $v){
			$this->arr_id[$i][]=$v['id'];
			$this->arr_id($v['id'],$i);
			
		}
		return $this->arr_id;
	}
	
	//根据id集合的每一个id，查询总消费
	function arr_fx_money($arr_id){
		foreach($arr_id as $k=>$v){
			$num=count($v);
			for($i=0;$i<$num;$i++){
//				$this->arr_fx_money[$k][$v[$i]]=$this->self_xf($v[$i]);
				$this->arr_fx_money[$k] += $this->self_xf($v[$i]);
			}
		}
		return $this->arr_fx_money;
	}
	
	
}




?>